package com.easy.common.util;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * HTTP请求取参数工具类
 * 
 */
public class RequestHelper {

	private static Logger logger = LoggerFactory.getLogger(RequestHelper.class);

	/**
	 * 取字符串，没有默认返回空字符串
	 * 
	 * @param request
	 * @param p
	 * @return
	 */
	public static String getParameter(HttpServletRequest request, String p) {
		return getParameter(request, p, "");
	}

	/**
	 * 取字符串
	 * 
	 * @param request
	 * @param p
	 * @return
	 */
	public static String getParameter(HttpServletRequest request, String p, String def) {
		String value = request.getParameter(p);
		if (StringUtils.isBlank(value)) {
			value = def;
		}
		return StringUtils.trim(value);
	}

	/**
	 * 取int类型
	 * 
	 * @param request
	 * @param p
	 * @param def
	 * @return
	 */
	public static int getParameter(HttpServletRequest request, String p, int def) {
		int val = 0;
		String value = request.getParameter(p);
		if (StringUtils.isBlank(value)) {
			val = def;
		} else {
			try {
				val = Integer.parseInt(value);
			} catch (Exception e) {
				logger.error("转换int类型参数错误，输入为[" + val + "]", e);
				val = 0;
			}
		}
		return val;
	}

	/**
	 * 取long类型
	 * 
	 * @param request
	 * @param p
	 * @param def
	 * @return
	 */
	public static long getParameter(HttpServletRequest request, String p, long def) {
		long val = 0;
		String value = request.getParameter(p);
		if (StringUtils.isBlank(value)) {
			val = def;
		} else {
			try {
				val = Long.parseLong(value);
			} catch (Exception e) {
				logger.error("转换long类型参数错误，输入为[" + val + "]", e);
				val = 0;
			}
		}
		return val;
	}

	/**
	 * 取float类型
	 * 
	 * @param request
	 * @param p
	 * @param def
	 * @return
	 */
	public static float getParameter(HttpServletRequest request, String p, float def) {
		float val = 0;
		String value = request.getParameter(p);
		if (StringUtils.isBlank(value)) {
			val = def;
		} else {
			try {
				val = Float.parseFloat(value);
			} catch (Exception e) {
				logger.error("转换float类型参数错误，输入为[" + val + "]", e);
				val = 0;
			}
		}
		return val;
	}

	/**
	 * 取double类型
	 * 
	 * @param request
	 * @param p
	 * @param def
	 * @return
	 */
	public static double getParameter(HttpServletRequest request, String p, double def) {
		double val = 0;
		String value = request.getParameter(p);
		if (StringUtils.isBlank(value)) {
			val = def;
		} else {
			try {
				val = Double.parseDouble(value);
			} catch (Exception e) {
				logger.error("转换double类型参数错误，输入为[" + val + "]", e);
				val = 0;
			}
		}
		return val;
	}

	/**
	 * 取boolean类型
	 * 
	 * @param request
	 * @param p
	 * @param def
	 * @return
	 */
	public static boolean getParameter(HttpServletRequest request, String p, boolean def) {
		Boolean val = null;
		String value = request.getParameter(p);
		if (StringUtils.isBlank(value)) {
			val = def;
		} else {
			val = Boolean.parseBoolean(value);
		}
		return val;
	}

	/**
	 * 取日期
	 * 
	 * @param request
	 * @param p
	 * @param def
	 * @return
	 */
	public static Date getParameterDate(HttpServletRequest request, String p, Date def) {
		String val = getParameter(request, p, "");
		if (StringUtils.isNotBlank(val)) {
			Date date = null;
			try {
				date = DateUtils.parseDate(val, new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
			} catch (Exception e) {
				logger.error("转换日期参数错误，输入为[" + val + "]", e);
				date = null;
			}
			return date;
		} else {
			return def;
		}
	}

	/**
	 * 取字符串数组
	 * 
	 * @param request
	 * @param p
	 * @return
	 */
	public static String[] getParameterValues(HttpServletRequest request, String p) {
		return request.getParameterValues(p);
	}

	/**
	 * 字符串转map
	 * @param paramters
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getParameters(String paramters) {
		if (StringUtils.isBlank(paramters)) {
			return null;
		}
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> rtMap = null;
		try {
			rtMap = mapper.readValue(paramters, Map.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtMap;
	}
}
